package list;

import org.junit.Test;
import stream.Student;
import stream.UseGroupBy;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author
 * @Title: UseSort
 * @Description:
 * @date 2022/12/14 1:29 下午
 */
public class TestSort {

    @Test
    public void test01(){
        Student s1 = new Student(22,"理想");
        Student s2 = new Student(12,"李四");
        Student s3 = new Student(8,"阿虎");

        List<Student> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);


        /*
            kw:list.sort
            Comparator<Student> comparing = Comparator.comparing(e -> e.getName()); // function接口程序给你一个入参,要你返回一个出参
            list.sort(comparing); // 给list提供一个比较器 list本身被直接改变了
            System.out.println(list);
        */

        // kw:list.stream().sorted  stream().sorted() 不会破坏list的排序
        List<Student> resultList = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
        System.out.println("resultList:"+resultList);
        System.out.println("list:"+list);
    }

    @Test
    public void test02(){
        List<String> strList = new ArrayList<>();
        strList.add("zo");
        strList.add("ab");
        strList.add("laaaaa");
        //Collections.sort(strList);
        strList.sort(Comparator.reverseOrder());
        System.out.println(strList);
    }

    /**
     * 倒序排列 只要实现了Comparable接口就可以使用Comparator.reverseOrder()倒序
     */
    @Test
    public void test03(){
        List<BigDecimal> list = Arrays.asList(BigDecimal.valueOf(12.3), BigDecimal.valueOf(12.1), BigDecimal.valueOf(15));
        list.sort(Comparator.reverseOrder());
        System.out.println(list);
    }

    /**
     *日文也支持排序的哦
     */
    @Test
    public void test04(){
        List<String> list = Arrays.asList("ドミニカ", "米国", "コンゴ民主共和国", "ガボン");
        list.sort(Comparator.naturalOrder());
        System.out.println(list);
    }



}
